package cn.rkylin.oms.area.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.area.domain.TopArea;
import cn.rkylin.oms.area.service.ITopAreaService;

@Controller
@RequestMapping("/area")
public class AreaController extends ApolloController{
    private static final Log logger = LogFactory.getLog(AreaController.class);
    
    @Autowired
    public ITopAreaService topAreaService;
    
    /**
     * 取得省
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getProvList", method = RequestMethod.GET)
    public Map<String, Object> getProvList() throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
         // 用于返回值的json对象
            Map<String , Object> resultMap = new HashMap<String , Object>();
            TopArea topArea = new TopArea();
            topArea.setParentCode("1");
            List<TopArea> list = topAreaService.findAreaList(topArea);
            // 设置返回结果内容
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, list);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }
    
    /**
     * 取得市
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getCityList", method = RequestMethod.GET)
    public Map<String, Object> getCityList(String provId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
         // 用于返回值的json对象
            Map<String , Object> resultMap = new HashMap<String , Object>();
            TopArea topArea = new TopArea();
            topArea.setParentCode(provId);
            List<TopArea> list = topAreaService.findAreaList(topArea);
            // 设置返回结果内容
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, list);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }
    
    /**
     * 取得区
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getDistList", method = RequestMethod.GET)
    public Map<String, Object> getDistList(String cityId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
         // 用于返回值的json对象
            Map<String , Object> resultMap = new HashMap<String , Object>();
            TopArea topArea = new TopArea();
            topArea.setParentCode(cityId);
            List<TopArea> list = topAreaService.findAreaList(topArea);
            // 设置返回结果内容
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, list);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }
}
